#!/bin/sh
# postrm script for minidlna

set -e

USER=minidlna
GROUP=${USER}
HOME=`getent passwd ${USER} | awk -F: '{ print $6 }'`
CONFIG=/etc/minidlna.conf
PIDDIR=/var/run/minidlna

# Based on the mediatomb debian package.

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

case "$1" in
    remove)
        rm -rf ${PIDDIR}
        ;;

    purge)
        # Only remove the files we expect minidlna to have created, in case the
        # user stored something valuable there.
        for dir in "${HOME}" /var/lib/minidlna; do
            rm -rf "${dir}/art_cache"
            rm -f  "${dir}/files.db"
            [ -d "${dir}" ] && rmdir --ignore-fail-on-non-empty "${dir}"
        done
        rm -f /var/log/minidlna.log ${CONFIG}

        dpkg-statoverride --quiet --remove ${CONFIG} || true
        dpkg-statoverride --quiet --remove /var/lib/minidlna || true

        if getent passwd ${USER} >/dev/null; then
            echo -n "Removing user '${USER}'..."
            deluser --quiet --system ${USER} && echo " Done." || echo " Failed."
        fi
        if getent group ${GROUP} >/dev/null; then
            echo -n "Removing group '${GROUP}'..."
            delgroup --quiet --system --only-if-empty ${GROUP} && echo " Done." || echo " Failed."
        fi

        # Remove minidlna user and group
		omv_config_delete_user minidlna
		omv_config_delete_group minidlna

		omv_config_delete "//services/minidlna"

        ;;

    upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
        ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#

exit 0
